package com.jic.data.service.impl;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import com.jic.data.exception.AppRuntimeException;
import com.jic.data.model.vo.response.ProductSaleDetailResponse;
import com.jic.data.model.vo.response.HistogramChartResponse;
import com.jic.data.model.vo.response.SaleDetailResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.jic.data.model.vo.response.SaleCategoryDetailResponse;
import com.jic.data.model.entity.SaleCategoryDetail;
import com.jic.data.mapper.SaleCategoryDetailMapper;
import com.jic.data.service.SaleCategoryDetailService;

import lombok.extern.slf4j.Slf4j;
/**
 * 接口实现类
 *
 * @author : 建投数据科技(山东)有限公司
 * @version : v1.0.0
 * @email :
 * @since : 2020-03-09 10:12:20
 */
@Service
@Slf4j
public class SaleCategoryDetailServiceImpl implements SaleCategoryDetailService {

	@Resource
	private SaleCategoryDetailMapper mapper;




	/**
	 * 商品销售-品类-90 - 销售品类明细
	 *
	 * @param request 不分页查询入参实体类
	 * @return 数据库查到的实体类列表
	 */
	@Override
	public List<SaleDetailResponse<ProductSaleDetailResponse<List<SaleCategoryDetailResponse>>>> listCategorySaleDetail(SaleCategoryDetail request) {
		List<SaleCategoryDetail> list = mapper.selectBySelective(request);

		List<SaleDetailResponse<ProductSaleDetailResponse<List<SaleCategoryDetailResponse>>>> saleDetailResponseList = new ArrayList<>();
		Map<Date,List<SaleCategoryDetail>> groupbyDate = list.stream().collect(Collectors.groupingBy(SaleCategoryDetail :: getSaleDate));

		groupbyDate.keySet().forEach(item -> {
			SaleDetailResponse<ProductSaleDetailResponse<List<SaleCategoryDetailResponse>>>  saleDetailResponse
					= new SaleDetailResponse();
			saleDetailResponse.setDate(DateUtil.format(item,"yyyy年MM月"));

			List<SaleCategoryDetail> detailList = groupbyDate.get(item);
			Map<String,List<SaleCategoryDetail>> groupbyStore = detailList.stream().collect(Collectors.groupingBy(SaleCategoryDetail :: getStoreCode));

			groupbyStore.keySet().forEach(temp -> {
				ProductSaleDetailResponse<List<SaleCategoryDetailResponse>> productDetailResponse =  new ProductSaleDetailResponse();

				List<SaleCategoryDetail> gyStore = groupbyStore.get(temp);
				List<SaleCategoryDetailResponse> responseList = gyStore.stream().map(temp1 -> {
					SaleCategoryDetailResponse bo = new SaleCategoryDetailResponse();
					BeanUtils.copyProperties(temp1, bo);
					return bo;
				}).filter(item1 -> item1 != null).collect(Collectors.toList());

				productDetailResponse.setStoreCode(temp);
				productDetailResponse.setProductList(responseList);

				saleDetailResponse.getList().add(productDetailResponse);
			});
			saleDetailResponseList.add(saleDetailResponse);
		});
		return saleDetailResponseList;
	}


	/**
	 * 商品销售品类90天-热销品类 TOP 10
	 * @param request
	 * @return
	 */
	@Override
	public HistogramChartResponse listTopCategory(SaleCategoryDetail request){
		HistogramChartResponse response = new HistogramChartResponse();
		List<String>  categoryName = new ArrayList<>();
		List<Double> saleAmount = new ArrayList<>();

		List<SaleCategoryDetail> list = mapper.listTopCategory(request);
		list.stream().forEach(item -> {
			categoryName.add(item.getCategoryName());
			saleAmount.add(item.getTotalSaleAmount().doubleValue());

		});
		response.setX(categoryName);
		response.setY1(saleAmount);
		return response;
	}

	/**
	 * 不分页查询
	 *
	 * @param request 查询入参实体类
	 * @return 实体类列表
	 */
	@Override
	public List<SaleCategoryDetail> listAllRecord(SaleCategoryDetail request) {
		return mapper.selectBySelective(request);
	}


}
